package com.freejy.ecommunity.web.controller;

import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.freejy.ecommunity.common.Token;
import com.freejy.ecommunity.common.word.NgWordUtil;
import com.freejy.ecommunity.model.GameInfo;
import com.freejy.ecommunity.model.Topic1;
import com.freejy.ecommunity.service.AccountService;
import com.freejy.ecommunity.service.GameInfoService;
import com.freejy.ecommunity.service.Topic1Service;
import com.freejy.ecommunity.utils.BeanMapDozer;
import com.freejy.ecommunity.utils.ContentEnum;
import com.freejy.ecommunity.utils.ContentLevelEnum;
import com.freejy.ecommunity.utils.RepostedEnum;
import com.freejy.ecommunity.utils.ResultCode;
import com.freejy.ecommunity.web.dto.request.GameInfoDto;
import com.freejy.ecommunity.web.dto.request.TopicReqDto;

/**
 * @author Jerry
 */
@RestController
@RequestMapping("/game/info")
public class GameInfoController extends BaseController
{
	
	private final Logger log = LoggerFactory.getLogger(GameInfoController.class);

    @Autowired
    private Topic1Service topicService;

    @Autowired
    private GameInfoService gameInfoService;
    
    /**
     * 发布动态
     * @param token
     * @param dto
     * @param result
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> post(@ModelAttribute("TokenCheck")Token token, @Valid @RequestBody TopicReqDto dto, BindingResult result) throws Exception
    {
        if (result.hasErrors())
        {
            return error(ResultCode.INVALID);
        }
        ContentEnum contentType = ContentEnum.getEnum(dto.getType());
        RepostedEnum repostedType = RepostedEnum.getEnum(dto.getReposted());
        if (contentType == null || repostedType == null) {
            return error(ResultCode.INVALID);
        }
        if ((contentType.getValue() == ContentEnum.TEXT.getValue() && StringUtils.isBlank(dto.getContent())) || (contentType.getValue() > ContentEnum.TEXT.getValue() && StringUtils.isBlank(dto.getShowUrls())))
        {
            return error(ResultCode.INVALID);
        }
        if (NgWordUtil.getInstance().isContentKeyWords(dto.getContent() + dto.getTags()))
        {
            return error(ResultCode.NGWORD_ERROR);
        }
        // parentId valid
        if (dto.getReposted().intValue() == RepostedEnum.REPRINTED.getValue() ) {
            if (dto.getParentId() == null)
            {
                return error(ResultCode.INVALID);
            }
            Topic1 topic = topicService.findById(dto.getParentId());
            if (topic == null)
            {
                return error(ResultCode.INVALID);
            }
        }
        //TODO save topic
        Topic1 topic = BeanMapDozer.map(dto, Topic1.class);
        topic.setUid(token.getUid());
        topic.setUserAgent(getUserAgent());
//        topic.setUserAgent(headerDto.getUser_agent());
        topic.setLevel(ContentLevelEnum.UNKNOWN.getValue());
        boolean res = topicService.save(topic);
        if (res)
        {
            return success(topic);
        } else {
            return error(ResultCode.FAIL);
        }
    }

    /**
     * 用户帖子列表
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list/{uid}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> ulist(@ModelAttribute("TokenCheck")Token token, @PathVariable("uid") Long uid) throws Exception
    {
        return success();
    }

    /**
     * 广场帖子列表
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/square", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> square() throws Exception
    {
        return success();
    }

    /**
     * 圈子帖子列表
     * @param token
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/circle", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> circle(@ModelAttribute("TokenCheck")Token token) throws Exception
    {
        return success();
    }

    /**
     * 热门帖子列表
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/hots", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> hots() throws Exception
    {
        return success();
    }

    @RequestMapping(value = "/hotss")
    public ResponseEntity<?> hotss() throws Exception
    {
    	return success(gameInfoService.getGameInfo());
    }
    
    
    @RequestMapping(value = "/homePage", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> homePage() throws Exception
    {
    	return success(gameInfoService.getHomePageGameInfo());
    }
    
    
    /**
     * 帖子详情
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getGameInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> getGameInfo() throws Exception
    {
    	
    	return success(gameInfoService.getGameInfo());
    }
    
    @RequestMapping(value = "/saveGameInfo", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> saveGameInfo(@Valid @RequestBody String  gameinfo) throws Exception
    {
    	log.info("接收参数："+gameinfo);
    	JSONObject json=JSONObject.parseObject(gameinfo);
    	GameInfo gameInfo = JSONObject.toJavaObject(json, GameInfo.class);
    	//GameInfo gameInfo = new GameInfo();
    	//gameInfo.setGameTags(gameinfo);
    	//GameInfoDto dto = null;
    	//GameInfo gameInfo = BeanMapDozer.map(dto, GameInfo.class);
    	return success(gameInfoService.insert(gameInfo));
    }

    /**
     * 删除帖子
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<?> delete(@ModelAttribute("TokenCheck")Token token, @PathVariable("id") Long id) throws Exception
    {
        return success();
    }

}
